package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.adventure.AdventureType;
import com.xcity.game.adventure.AdventureUtils;
import com.xcity.game.adventure.model.ActionTarget;
import com.xcity.game.adventure.model.Adventure;
import com.xcity.game.adventure.template.AdventureTemplate;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.item.ItemTemplate;
import com.xcity.pomelo.response.AdventurePlayResponse.Target;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class AdventureInfoResponse extends Response {

	public int pos; // 当前位置
	public int str; // 消耗体力
	public List<Prize> list; // 奖品列表
	public NotExecuteAction lastAction; // 上次未执行的动作（偷窃、攻击）
	public Target nextStealTarget; // 下一次偷取目标
	
	public static class Prize {
		public AdventureType type; // 类型
		public int id; // 类型为物品时表示物品id
		public int count; // 类型为物品时表示物品数量，为其他时表示对应的数值
		public String name = "";
		public String icon = "";
		public String desc = "";
	}
	
	public static class NotExecuteAction {
		public AdventureType type;
		public List<Target> targets;
		public NotExecuteAction(Adventure adv) {
			this.type = adv.getLastAction();
			List<ActionTarget> __targets__ = adv.getLastActionTargets();
			this.targets = new ArrayList<>(__targets__.size());
			for (int i = 0; i < __targets__.size(); i++) {
				this.targets.add(new Target(__targets__.get(i)));
			}
		}
	}
	
	public AdventureInfoResponse(int serial, Player player, int str, List<AdventureTemplate> templates) {
		super(serial);
		this.pos = player.getPool().getIntValue(PlayerPool.PROPERTY_ADVENTURE_POSITION);;
		this.str = str;
		int size = templates.size();
		this.list = new ArrayList<Prize>(size);
		TemplateService service = App.getApp().getServiceManager().get(TemplateService.class);
		for (int i = 0; i < size; i++) {
			Prize p = new Prize();
			AdventureTemplate template;
			if (i == GameStaticConfig.adventureCaptureFuncPos
					&& AdventureUtils.getAdventureCaptureFuncCount(player) >= 0) {
				template = AdventureTemplate.capture;
			} else {
				template = templates.get(i);
			}
			p.type = template.getType();
			if (template.getType() == AdventureType.ITEM) {
				p.id = template.getData();
				p.count = 1;
				ItemTemplate item = service.get(ItemTemplate.class, p.id);
				p.name = item.getName();
//				p.icon = item.getIcon();
				p.desc = item.getDesc();
			} else {
				p.id = template.getType().ordinal();
				p.count = template.getData();
				p.name = template.getType().name;
				p.desc = template.getType().desc;
			}
			p.icon = template.getIcon() != null ? template.getIcon() : "";
			this.list.add(p);
		}
		AdventureType t = player.getAdventure().getLastAction();
		if (t == AdventureType.ATTACK || t == AdventureType.STEAL || t == AdventureType.CAPTURE) {
			this.lastAction = new NotExecuteAction(player.getAdventure());
		}
		this.nextStealTarget = new Target(player.getAdventure().getNextStealTarget());
	}

}
